package com.gcxy.tour.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gcxy.tour.common.CommException;
import com.gcxy.tour.common.JdbcUtils;
import com.gcxy.tour.domain.ProductInfo;

/**
 * 内容：产品Product信息的 增 删 改 查
 * @author 方昆
 *
 */

public class ProductInfoDao {

	private Logger logger=LoggerFactory.getLogger(getClass());
	/**
	 * 获取数据库中产品Product的id Author Productname Productcode
	 * @param rs
	 * @return
	 * @throws SQLException
	 */
	private ProductInfo rowMap(ResultSet rs) throws SQLException {
		ProductInfo productInfo=new ProductInfo();
		productInfo.setId( rs.getInt("id"));
		productInfo.setAuthor(rs.getString("Author"));
		productInfo.setProductname(rs.getString("Productname"));
		productInfo.setProductcode(rs.getString("Productcode"));
		return productInfo;
	}
	/**
	 * 根据标号在数据库中查询该产品信息
	 * @param productcode
	 * @return
	 */
	public ProductInfo findByCode(String productcode) {
		Connection conn=JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			String sql="select * from product where productcode=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, productcode);
			
			logger.debug(sql);
			
			rs=ps.executeQuery();
			while (rs.next()) {
				return rowMap(rs);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new CommException(e.getMessage());
		} finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return null;
	}
	/**
	 * 根据账号修改数据库表的记录
	 * @param product
	 */
	public void update(ProductInfo productInfo) {
		Connection conn=JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			String sql="update product set productname=?,author=? where productcode=?";
			ps=conn.prepareStatement(sql);
			ps.setString(1, productInfo.getProductname());
			ps.setString(2, productInfo.getAuthor());
			ps.setString(3, productInfo.getProductcode());
			
			logger.debug(sql);
			
			if(ps.executeUpdate()<1) {
				throw new CommException("修改数据失败");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new CommException(e.getMessage());
		} finally {
			JdbcUtils.close(conn, ps, rs);
		}
		
	}
	/**
	 * 往数据库中插入产品信息 productname,author,productcode
	 * @param productInfo
	 */
	public void insert(ProductInfo productInfo) {
		Connection conn=JdbcUtils.getConnection();
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			String sql="insert into product(productname,author,productcode) values(?,?,?)";
			ps=conn.prepareStatement(sql);
			ps.setString(1, productInfo.getProductname());
			ps.setString(2, productInfo.getAuthor());
			ps.setString(3, productInfo.getProductcode());
			
			logger.debug(sql);
			
			if(ps.executeUpdate()<1) {
				throw new CommException("插入数据失败");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			throw new CommException(e.getMessage());
		} finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
}
